<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2018/12/10
 * Time: 17:27
 */

namespace app\admin\controller;

use app\Model\PickupModel;
use app\Model\SuppliersModel;
class Pickup extends Common
{
    /**
     * @var int
     * 返回数据到自提点页面
     */
    private $page = 20;
 public function pickup_list(){
     $this->getConAndAct('pick');
     $res['list'] = PickupModel::alias('p')->field('p.*')
         ->join('tp_suppliers su','su.suppliers_id = p.suppliersid AND p.suppliersid !=0','LEFT')
         ->field('su.suppliers_name')
         ->order('p.pickup_id desc')
         ->paginate($this->page);
     return view('index',$res);
 }

    /**
     * @return \think\response\View
     * 跳转自提点添加页面
     */
    public function pickup_add(){
        $data['list'] = SuppliersModel::field('suppliers_id,suppliers_name')->where('is_check','=',1)->select();
     return view('add',$data);
    }

    /**
     * @return \think\response\Json
     * 新增自提点
     */
    public function add(){
        //数据验证
        $this->veri('pickup','add');
        $ret = PickupModel::create([
            'pickup_name'        => input('pickup_name'),
            'pickup_address'         => input('pickup_address'),
            'pickup_phone'       =>  input('pickup_phone'),
            'pickup_contact'      => input('pickup_contact'),
            'province'          => input('province'),
            'city'       =>input('city'),
            'district'       =>input('district'),
            'suppliersid'       =>input('suppliersid')
        ]);
        if(!$ret){
            return retJson('', 201, '新增失败');
        }
        return retJson('', 200, '新增成功');
    }

    /**
     * @return \think\response\View
     * 搜索自提点
     */
    public function pickup_search(){
         $where=[];
        if (input('pickup_name')){
            $where['pickup_name']=input('pickup_name');
        }elseif(input('province')){
            $where['p.province']=input('province');
        }elseif (input('city')){
            $where['city']=input('city');
        }elseif (input('district')){
            $where['district']=input('district');
        }

        $res['list'] = PickupModel::alias('p')->field('p.*')
            ->join('tp_suppliers su','su.suppliers_id = p.suppliersid AND p.suppliersid IS NULL','LEFT')
            ->field('su.suppliers_name')
            ->where($where)
            ->order('p.pickup_id desc')
            ->paginate($this->page);
        return view('index',$res);
    }

    /**
     * @return \think\response\Json
     * 删除自提点
     */
    public function del(){

        $del = PickupModel::destroy(input('id'));
        if(!$del){
            return retJson('', 201, '删除失败！');
        }
        return retJson('', 200, '删除成功！');
    }

    /**
     * @return \think\response\View
     * 跳转编辑页面，返回编辑参数
     */
    public function pickup_edit(){
        $info['list'] = SuppliersModel::field('suppliers_id,suppliers_name')->where('is_check','=',1)->select();

        $data= PickupModel::field('*')->where('pickup_id',input('pickup_id'))
            ->find();
        $this->assign('data',$data);
        return view('edit',$info);
    }


    /**
     * @return \think\response\Json
     * 修改自提点
     */
    public function edit(){
        //数据验证
        $this->veri('pickup','edit');

        $data['pickup_name']   = input('pickup_name');
        $data['pickup_address']   = input('pickup_address');
        $data['pickup_phone']= input('pickup_phone');
        $data['pickup_contact'] = input('pickup_contact');
        $data['province']   = input('province');
        $data['city']   = input('city');
        $data['district']   = input('district');
        $data['suppliersid']   = input('suppliersid');

        // 自提点信息修改
        $res = PickupModel::update($data, ['pickup_id' => input('pickup_id')]);
        if(!$res){
            return retJson('', 201, '修改失败');
        }

        return retJson('', 200, '修改成功');
    }

    /**
     * 添加自提点页面跳转
     */
    public function pickupadd(){
        $this->getConAndAct('pick');
        $this->pickup_add();
    }
}